﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using System.IO;

public partial class ProductHot : SitePage
{
    //------------------------------------------------
    //VARIABLES
    //------------------------------------------------
    protected int TotalProducts = 0;
    //------------------------------------------------
    //METHODS
    //------------------------------------------------
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitData();
        
    }

    private void InitData()
    {
        this.pagerTop.PageIndex=pagerBottom.PageIndex = 1;
        this.pagerTop.PageSize = this.pagerBottom.PageSize = clsConfigData.DEFAULT_PRODUCT_PAGE_SIZE;

    }

    private void _LoadContentPage()
    {
        busContentPage objbus = new busContentPage();
        ContentPage objData = objbus.GetByURL("products");
        this.SetMetaData(string.Format(Resources.Resource.TITLE, Server.HtmlEncode(objData.Name)), objData.MetaDescription, objData.MetaKeywords);
        ltrContentPage.Text = objData.Content;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            _LoadContentPage();
            LoadListProduct();
        }
    }

    private void LoadListProduct()
    {
        ProductCollection lstProduct = new ProductCollection();
        busProduct objBus = new busProduct();

        //get Condition
        Product objProduct = new Product();
        objProduct.CurrentPage = this.pagerTop.PageIndex;
        objProduct.OrderBy = busProduct.CREATEDDATE;
        objProduct.OrderDirection = "DESC";
        objProduct.PageSize = pagerTop.PageSize;
        objProduct.Condition = "p.IsActive=1 and p.IsNew=1 ";
        if (clsCheck.QueryString("z", enmDataType._Int))
        {
            objProduct.Condition += " and ZoneId=" + clsString.QueryString("z");
        }

        lstProduct = objBus.GetListAll(objProduct);

        TotalProducts = 0;
        if (lstProduct != null && lstProduct.Count > 0)
        {
            TotalProducts = lstProduct.Count;
            dalProduct.DataSource = lstProduct;
            dalProduct.DataBind();
            pagerTop.SetPaging(TotalProducts, objProduct.TotalRecords);
            pagerBottom.SetPaging(TotalProducts, objProduct.TotalRecords);
            pagerTop.Visible = true;
            pagerBottom.Visible = true;
        }
        else
        {
            pagerTop.Visible = false;
            pagerBottom.Visible = false;
        }
    }

    protected void dalProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        DataListItem item = e.Item;
        Product data = new Product();
        if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
        {
            if (data == null)
                return;

            data = (Product)item.DataItem;
            PlaceHolder phdControl = (PlaceHolder)item.FindControl("phdControlProduct");
            UserControl ctrl = new UserControl();
            ucProductItem uc = (ucProductItem)ctrl.LoadControl("~/UserControl/ProductItem.ascx");
            if (uc != null)
            {
                uc.ProductItem = data;
            }
            phdControl.Controls.Add(uc);
        }
    }

    protected void pagerTop_PageIndexChanging(object obj, CommonTagTime.PagerControl.PageIndexChangeEventArgs e)
    {
        this.pagerTop.PageIndex = pagerBottom.PageIndex = e.PageIndex;        
        LoadListProduct();
    }

    protected void pagerTop_PageSizeChanging(object obj, CommonTagTime.PagerControl.PageSizeChangeEventArgs e)
    {
        this.pagerTop.PageIndex = pagerBottom.PageIndex = 1;
        this.pagerTop.PageSize = this.pagerBottom.PageSize = e.PageSize;
        LoadListProduct();
    }
}